package com.buyingtickets.controller;

import com.buyingtickets.dao.InitialSeatDao;
import com.buyingtickets.dao.SeatDao;
import com.buyingtickets.entity.InitialSeat;
import com.buyingtickets.entity.Train;
import com.buyingtickets.service.TrainNumberService;
import com.buyingtickets.service.TrainService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("train")
public class AdminRegionController {
    @Resource
    private TrainService trainService;
    @Resource
    private TrainNumberService trainNumberService;

    @RequestMapping("addtrainnum/{trainid}/{date}/{time}")
    @ResponseBody
    public String addtrainnum(@PathVariable("trainid") Integer trainid, @PathVariable("date") String date, @PathVariable("time") String time,Model model) {//添加车次

            if (trainNumberService.isTrainNumber(trainid, date, time) == null) {
                if(trainNumberService.addTrainNum(trainid, date, time)){
                    return "0";//添加成功
                }else{
                    return "1";//车次存在
                }
            }

        return "2";//添加失败
    }


    @RequestMapping("{tid}/tnadd")
    public String update(@PathVariable("tid") Integer id, Model model) {//车次详情页

            model.addAttribute("train", trainService.queryByTrainID(id));

        return "admin_region_details";
    }
    //前往路线添加界面，显示列车表
    @RequestMapping("{page}/toAdminTrain")
    public String  addRoute(@PathVariable("page") String str, Model model){
        int page = 1;
        try {
            page = Integer.valueOf(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int count = trainService.queryCount();
        pageUitl(page, count, "",model);
        model.addAttribute("pageFlag", 1);
        return "admin_train";
    }

    //模糊查询列车,翻页
    @RequestMapping("{tid}/{page}/querytrainbyid")
    public String queryTrainByIDOrName(@PathVariable("tid") String tid, @PathVariable("page") String page, Model model) {
        int page1 = 0;
        model.addAttribute("pageFlag", 0);
        try {
            page1 = Integer.valueOf(page);
        } catch (Exception e) {
            e.printStackTrace();
            int count = trainService.queryCount();
            pageUitl(1, count,tid, model);
            return "admin_region";
        }
        if (StringUtils.hasText(tid)) {
            int count = trainService.queryCountByName(tid);
            List<Train> trains = trainService.queryByTrainIDOrName(tid, page1);
            int size = trains.size();
            int pages = count % 5 == 0 ? (count / 5) : (count / 5 + 1);
            trainUitl(1, count, pages, size, trains,tid, model);
        } else {
            int count = trainService.queryCount();
            pageUitl(1, count, tid,model);
        }

        return "admin_region";
    }

    @RequestMapping("{page}/toAdminRegion")
    public String toAdminRegion(@PathVariable("page") Integer page, Model model) {//前往车次添加界面，显示列车表
        int count = trainService.queryCount();
        pageUitl(page, count, "",model);
        model.addAttribute("pageFlag", 1);//判断网页跳转
        return "admin_region";
    }

    private void pageUitl(int page, int count,String query, Model model) {
        List<Train> trains = trainService.queryByTrainAll(page);
        int size = trains.size();
        int pages = count % 5 == 0 ? (count / 5) : (count / 5 + 1);
        trainUitl(page, count, pages, size, trains, query,model);
    }

    private void trainUitl(int page, int count, int pages, int size, List<Train> trains, String query,Model model) {
        model.addAttribute("cspage", page);//
        model.addAttribute("trainsList", trains);//列车集合
        model.addAttribute("trainsListSize", size);//单页条数
        model.addAttribute("trainsCount", count);//总条数
        model.addAttribute("trainPages", pages);//总页数
        model.addAttribute("trainquery",query);//模糊查询字符串
    }

}
